﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		TrackBackService.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion
 
using System;
using Dexter.Core.EventArgs;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.Repository;
using Dexter.Services;

namespace Dexter.Services.Implementation {
	/// <summary>
	/// 	The implementation of <see cref = "ITrackBackService" />.
	/// </summary>
	public class TrackBackService : ITrackBackService {
		readonly ILogger logger;
		readonly IPostRepository postRepository;
		readonly ITrackBackRepository trackBackRepository;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "TrackBackService" /> class.
		/// </summary>
		/// <param name = "logger">The logger.</param>
		/// <param name = "trackBackRepository">The track back repository.</param>
		/// <param name = "postRepository">The post repository.</param>
		public TrackBackService ( ILogger logger , ITrackBackRepository trackBackRepository , IPostRepository postRepository ) {
			this.logger = logger;
			this.postRepository = postRepository;
			this.trackBackRepository = trackBackRepository;
		}

		/// <summary>
		/// 	Adds the <see cref = "TrackBack" /> for the specified post.
		/// </summary>
		/// <param name = "itemId">The item id of the post.</param>
		/// <param name = "website">The website of the trackback.</param>
		/// <param name = "title">The title.</param>
		public virtual void AddTrackBack ( int itemId, Uri website, string title ) {
			var tupla = new Tuple <int , Uri , string> ( itemId , website , title );
			var ea = new CancelEventArgsWithOneParameter <Tuple <int , Uri , string>> ( tupla );
			TrackBackAdding.Raise ( this , ea );

			if ( ea.Cancel ) {
				return;
			}

			var item = postRepository.GetByKey ( itemId );

			TrackBack data = TrackBack.CreateNewTrackback ( item , website , title );

			trackBackRepository.Add ( data );

			TrackBackAdded.Raise ( this , new GenericEventArgs <TrackBack> ( data ) );
		}

		/// <summary>
		/// 	Determines whether is first ping back for the specified post slug.
		/// </summary>
		/// <param name = "postKey">The post key.</param>
		/// <param name = "url">The URL.</param>
		/// <returns>
		/// 	<c>true</c> if is first ping back; otherwise, <c>false</c>.
		/// </returns>
		public virtual bool IsFirstTrackbackBack ( int postKey, Uri url ) {
			return trackBackRepository.IsFirstPingBack ( postKey , url );
		}

		/// <summary>
		/// 	This event will raise before a new <see cref = "TrackBack" /> processed by the implementation of <see cref = "AddTrackBack" />.
		/// </summary>
		public virtual event EventHandler<CancelEventArgsWithOneParameter<Tuple<int, Uri, string>>> TrackBackAdding;

		/// <summary>
		/// 	This event will raise after a new <see cref = "TrackBack" /> processed by the implementation of <see cref = "AddTrackBack" />.
		/// </summary>
		public virtual event EventHandler<GenericEventArgs<TrackBack>> TrackBackAdded;
	}
}
